#!/bin/sh

function addTapDeviceToBridge() {
	tap_interface_bridged=$(uci show network.lan.ifname | grep tap_homelede | wc -l)
	[ $tap_interface_bridged -eq 0 ] && {
		old_lan_ifname=$(uci show network.lan.ifname | cut -d \' -f 2)
		lan_ifname="$old_lan_ifname tap_homelede"
		eval "uci set network.lan.ifname='$lan_ifname'"
		uci commit network
		/etc/init.d/network restart
	}
}

function removeTapDeviceFromBridge() {
	tap_interface_bridged=$(uci show network.lan.ifname | grep tap_homelede | wc -l)
	[ $tap_interface_bridged -eq 1 ] && {
		old_lan_ifname=$(uci show network.lan.ifname | cut -d \' -f 2)
		lan_ifname=${old_lan_ifname// tap_homelede/}
		eval "uci set network.lan.ifname='$lan_ifname'"
		uci commit network
		/etc/init.d/network restart
	}
}

addTapDeviceToBridge

uci -q batch <<-EOF >/dev/null
	delete firewall.homeconnect
	set firewall.homeconnect=include
	set firewall.homeconnect.type=script
	set firewall.homeconnect.path=/usr/share/homeconnect/firewall.include
	set firewall.homeconnect.reload=1
EOF

uci -q batch <<-EOF >/dev/null
	delete ucitrack.@homeconnect[-1]
	add ucitrack homeconnect
	set ucitrack.@homeconnect[-1].init=homeconnect
	commit ucitrack
EOF

/etc/init.d/softethervpnbridge disable && /etc/init.d/softethervpnbridge stop
/etc/init.d/softethervpnclient disable && /etc/init.d/softethervpnclient stop
/etc/init.d/softethervpnserver disable && /etc/init.d/softethervpnserver stop
chmod +x /etc/init.d/homeconnect
rm -rf /tmp/luci-*cache
exit 0
